package com.hm.oltu.auto;

import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.hm.common.util.CommonUtil;
import com.hm.common.util.StringUtil;
import com.hm.oltu.def.CommonDef;

@Aspect
@Component
public class ActionAspectHandler {

	@Around("execution(* com.hm.oltu.web.**Controller.*(..))")
	public Object process(ProceedingJoinPoint point) throws Throwable {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getRequest();
		HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
				.getResponse();

		String uri = request.getRequestURI();
		if (!Arrays
				.asList(new String[] { "/login", "/index", "", "/", "/logout", "/error/index", "/sessionError/index" })
				.contains(uri)) {
			if (CommonUtil.isEmpty(request.getSession()
					.getAttribute(CommonDef.BaseInit.SessionOrCookieToken.SESSION_LOGIN_USER_INFO))) {
				response.setHeader("Cache-Control", "no-store");
				response.setDateHeader("Expires", 0);
				response.setHeader("Prama", "no-cache");
				response.sendRedirect("index?code=" + StringUtil.token());
				return null;
			}
		}

		return point.proceed();
	}
}
